package com.lp.zyll.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lp.zyll.aop.Log;
import com.lp.zyll.domain.LpLog;
import com.lp.zyll.service.LpLogService;
import com.lp.zyll.mapper.LpLogMapper;
import com.lp.zyll.util.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;

/**
* @author lpden
* @description 针对表【lp_log(系统日志)】的数据库操作Service实现
* @createDate 2022-03-13 23:45:08
*/
@Service
public class LpLogServiceImpl extends ServiceImpl<LpLogMapper, LpLog>
    implements LpLogService{


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, LpLog log) {


        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Log aopLog = method.getAnnotation(Log.class);

        // 方法路径
        String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";

        // 描述
        if (log != null) {
            log.setDescription(aopLog.value());
        }
        assert log != null;
        log.setRequestIp(ip);

        log.setAddress(StringUtils.getCityInfo(log.getRequestIp()));
        log.setMethod(methodName);
        log.setUsername(username);

//        log.setParams(getParameter(method, joinPoint.getArgs()));
        log.setBrowser(browser);
        save(log);

    }



}




